package io.paper.utils;

import io.renren.entity.SysUserEntity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.alibaba.druid.util.StringUtils;

/**
 * Shiro工具类
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月12日 上午9:49:19
 */
public class CodeSetUtil {

	public static String getISOtoGBK(String ISO) {
		String res="";
		try {
			if(!StringUtils.isEmpty(ISO)){
				res = new String(ISO.getBytes("ISO8859_1"),"GBK");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public static String getGBKtoISO(String GBK) {
		String res="";
		try {
			if(!StringUtils.isEmpty(GBK)){
				res = new String(GBK.getBytes("GBK"),"ISO8859_1");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public static String getDbParam(String sql,String param,String paramName){
		sql=sql.replace("#{"+paramName+"}", param);
		return sql;
		
	}
	
	public static String changeDateFomat(String paramDate){
		String[] dateList=null;
		String returnStr="";
		if(!StringUtils.isEmpty(paramDate)){
			dateList=paramDate.split("-");
		}
		if(null != dateList && dateList.length==3){
			returnStr=String.format("%s/%s/%s", dateList[1],dateList[2],dateList[0]);
		}
		return returnStr;
	}
	
	public static void main(String[] args) {
		String sql = "select a,* from abc where #{a} = #{b} and #{a}=#{c} ORDER BY #{a} ";
		sql=getDbParam(sql,"123","a");
		sql=getDbParam(sql,"B","b");
		sql=getDbParam(sql,"C","c");
		System.out.println(sql);
	}

	public static String cellForString(Cell cell){
        if(cell == null){
            return "";
        } else {
            String value = "";
            switch (cell.getCellType()){
                case Cell.CELL_TYPE_NUMERIC:
                    value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString();
                    break;
                case Cell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue().trim();
                    if(!StringUtils.isEmpty(value) && value.startsWith("`")){
                    	value=value.substring(1, value.length());
                    }
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    value = cell.getCellFormula().trim();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    value = String.valueOf(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_ERROR:
                    value = String.valueOf(cell.getErrorCellValue());
                    break;
                default:
                    break;
            }
            return value;
        }
    }
	
	public static void inputStreamToFile(InputStream ins, File file) {
	    try {
	        OutputStream os = new FileOutputStream(file);
	        int bytesRead = 0;
	        byte[] buffer = new byte[8192];
	        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
	            os.write(buffer, 0, bytesRead);
	        }
	        os.close();
	        ins.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}
